#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int N = 200005, P = 1000000007;
int n, l[N], r[N], sf[N], sg[N], sh[N];
inline int Pow(ll x, int y=P-2){
int ans=1;
for(; y; y>>=1, x=x*x%P) if(y&1) ans=ans*x%P;
return ans;
}
int main() {
scanf("%d", &n);
for(int i=1; i<=n; ++i) scanf("%d", l+i);
for(int i=1; i<=n; ++i) scanf("%d", r+i);
sh[0]=1;
sh[1]=sg[1]=sf[1]=r[1]-l[1]+1;
for(int i=2; i<=n; ++i){
int len=r[i]-l[i]+1;
sh[i]=(ll)sh[i-1]*len%P;
int len2=max(min(r[i], r[i-1])-max(l[i], l[i-1])+1, 0);
sg[i]=((ll)(sg[i-1]+sh[i-1])*len-(ll)sh[i-2]*len2)%P;
int len3=max(min(min(r[i], r[i-1]), r[i-2])-max(max(l[i], l[i-1]), l[i-2])+1, 0);
sf[i]=((sf[i-1]+2ll*sg[i-1]+sh[i-1])*len-(2ll*sg[i-2]+3ll*sh[i-2])*len2+(i>2?2ll*sh[i-3]:0)*len3)%P;
}
printf("%lld\n", (ll)(sf[n]+P)*Pow(sh[n])%P);
return 0;
}
394. Decode String | 902. Numbers At Most N Given Digit Set |
221. Maximal Square | 1200. Minimum Absolute Difference |
1619B - Squares and Cubes | 1619A - Square String |
1629B - GCD Arrays | 1629A - Download More RAM |
1629C - Meximum Array | 1629D - Peculiar Movie Preferences |
1629E - Grid Xor | 1629F1 - Game on Sum (Easy Version) |
2148. Count Elements With Strictly Smaller and Greater Elements | 2149. Rearrange Array Elements by Sign |
2150. Find All Lonely Numbers in the Array | 2151. Maximum Good People Based on Statements |
2144. Minimum Cost of Buying Candies With Discount | Non empty subsets |
1630A - And Matching | 1630B - Range and Partition |
1630C - Paint the Middle | 1630D - Flipping Range |
1328A - Divisibility Problem | 339A - Helpful Maths |
4A - Watermelon | 476A - Dreamoon and Stairs |
1409A - Yet Another Two Integers Problem | 977A - Wrong Subtraction |
263A - Beautiful Matrix | 180C - Letter |